package xml;

import context.*;

import java.io.IOException;
import java.io.Reader;
import java.util.Hashtable;


//*****************************************************************************
//*** CLASS: XMLStreamReader
//*****************************************************************************
public class XMLStreamReader implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
//  private InputStream inputStream;
  private Reader reader;
  private String reportTagName;

  public interface Listener
  {
    public void treatXML(XMLElement elt);
  }
  
  // ***************************************************************************
  // *** CONSTRUCTOR: XMLStreamReader
  // ***************************************************************************
  public XMLStreamReader(Reader reader)
  { this.reader=reader; }
  
  // ***************************************************************************
  // *** METHOD: close
  // ***************************************************************************
  public void close() throws IOException
  { reader.close(); }
  
  // ***************************************************************************
  // *** METHOD: parse
  // ***************************************************************************
  public void parse(Listener listener,String reportTagName) throws IOException
  { this.reportTagName=reportTagName;
    parse(listener);  
  }

  public void parse(Listener listener) throws IOException
  { if(DEBUG) Debug.log("XMLStreamReader'run","XMLStreamReader -- Started");
    XMLElement currentElement=null;
//    MiniXmlParser parser=new MiniXmlParser(inputStream);
    MiniXmlParser parser=new MiniXmlParser(reader);
    try
    { parser.setRelaxed(true); // Aksepter div. errors
      int eventType=parser.getType();
      while(eventType!=MiniXmlParser.END_DOCUMENT)
      {
        if(eventType==MiniXmlParser.START_DOCUMENT) { System.out.println("Start document"); }
        else if(eventType == MiniXmlParser.END_DOCUMENT) { System.out.println("End document"); }
        else if(eventType == MiniXmlParser.START_TAG)
        { String name=parser.getName();
          //if(LOG_XML) Debug.log("XMLStreamReader.parse","<<  START: "+name);
          Hashtable attributes=new Hashtable();
          int i=parser.getAttributeCount();
          for(int j=0;j<i;j++)
          { String attr=parser.getAttributeName(j);
            String value=parser.getAttributeValue(j);
            attributes.put(attr,value);
          }
          if(reportTagName!=null)
          { if(name.equals(reportTagName)) currentElement=new XMLElement(null,name,attributes);
            else if(currentElement!=null) currentElement=new XMLElement(currentElement,name,attributes);
          } else currentElement=new XMLElement(currentElement,name,attributes);
        }
        else if(eventType == MiniXmlParser.TEXT)
        { String text=parser.getText();
          //if(LOG_XML) Debug.log("XMLStreamReader.parse","<<  TEXT: "+text);
          if(currentElement!=null) currentElement.addText(text);
        }
        else if(eventType == MiniXmlParser.END_TAG)
        { //String name=parser.getName();
          //if(LOG_XML) Debug.log("XMLStreamReader.parse","<<  END: "+parser.getName());
          if(currentElement!=null)
          { XMLElement parent=currentElement.getParent();
            if(parent==null)
            { if(LOG_XML) Debug.log("XMLStreamReader.parse","<<  READ: "+currentElement);
              listener.treatXML(currentElement);
            }
            else parent.addChild(currentElement);
            currentElement=parent;
          }
        }
        eventType=parser.next();
      }
    }
    catch(IOException e) { Debug.EXCEPTION("XMLStreamReader: ",e); }
  }

}
